package com.priox.sigar.application.job.mappers;

import com.priox.sigar.application.mappers.MonedaMapper;
import com.priox.sigar.client.dto.MonedaDTO;
import com.priox.sigar.client.dto.OperacionCuentaDTO;
import com.priox.sigar.enums.TipoCuenta;
import com.priox.sigar.util.NumberUtil;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindException;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * User: Matrix
 * Date: 10/02/13
 */
@Component
public class ResumenExcelFieldSetMapper implements FieldSetMapper<OperacionCuentaDTO> {
    private static final Logger LOGGER = LoggerFactory.getLogger(ResumenExcelFieldSetMapper.class);
    private Long checkPointId;
    private Long usuarioId;

    @Autowired
    MonedaMapper monedaMapper;

    /*
     * (non-Javadoc)
     *
     * @see
     * org.springframework.batch.item.file.mapping.FieldSetMapper#mapFieldSet
     * (org.springframework.batch.item.file.transform.FieldSet)
     */
    @Override
    public OperacionCuentaDTO mapFieldSet(FieldSet fieldSet) throws BindException {
        OperacionCuentaDTO result = new OperacionCuentaDTO();

        List<MonedaDTO> monedas = monedaMapper.findAll();
        Map<String, String> monedaSimbolo = new HashMap<String, String>();
        for (Iterator<MonedaDTO> iterator = monedas.iterator(); iterator.hasNext(); ) {
            MonedaDTO monedaDTO = iterator.next();
            monedaSimbolo.put(monedaDTO.getSimboloEmpresa(), monedaDTO.getSimboloSap());
        }

        String patternNumeros = "[+-]*[0-9,0-9]*[0-9.0-9]*";
         try {
            result.setUsuarioId(usuarioId);
            result.setCheckPointId(checkPointId);
            if(NumberUtils.isDigits(fieldSet.readString(0))){
                result.setNumeroDocumento(fieldSet.readString(0));
            }
            result.setTipoCuenta(TipoCuenta.CUENTA_RESUMEN);
            String moneda = monedaSimbolo.get(fieldSet.readString(1));
            if (moneda == null)
                result.setCodigoMoneda(fieldSet.readString(1));
            else
                result.setCodigoMoneda(moneda);

            result.setCuentaContableId("SISTEMA");
            if(StringUtils.isNotBlank(fieldSet.readString(2))){
                result.setAcreedorId("SISTEMA");
                result.setAcreedorDocumento(fieldSet.readString(2));
            }else{
                result.setAcreedorId("SISTEMA");
            }

            String columna=fieldSet.readString(3);
            if (StringUtils.isNotBlank(columna)&& columna.matches(patternNumeros)) {
                result.setMontoMonedaDolares(NumberUtil.getNumber(columna));
                result.setUsuario("SISTEMA");

            }

            result.setReferencia(fieldSet.readString(4));
            result.setSociedadId(fieldSet.readString(7));
        } catch (Exception ex) {
            LOGGER.error("Error al leer resumen de excel" + fieldSet);
        }
        return result;
    }

    public Long getCheckPointId() {
        return checkPointId;
    }

    public void setCheckPointId(Long checkPointId) {
        this.checkPointId = checkPointId;
    }

    public Long getUsuarioId() {
        return usuarioId;
    }

    public void setUsuarioId(Long usuarioId) {
        this.usuarioId = usuarioId;
    }
}

